package com.dmap.officialwebsite.controller.background;

import com.dmap.base.constants.Constant;
import com.dmap.base.vo.BaseVo;
import com.dmap.officialwebsite.controller.background.param.ArticleParam;
import com.dmap.officialwebsite.controller.background.param.ArticleSelectParam;
import com.dmap.officialwebsite.po.Article;
import com.dmap.officialwebsite.service.ArticleService;
import com.dmap.officialwebsite.vo.ArticleAndRecordNumVo;
import com.dmap.officialwebsite.vo.ArticleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 03/09/18
 * \* Description:
 * \
 */
@Api(value = "文章管理", description = "background/文章管理")
@Controller
@RequestMapping(value = "/background/dmap-officialwebsite-server/v1/article/")
public class ArticleController {

    @Resource
    private ArticleService articleService;

    @ResponseBody
    @ApiOperation(value = "add 文章",
            notes = "1.必填：companyId, companyName, articleTypeId, articleTypeName, title;" +
                    "2.返回值为：id")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public BaseVo insert(@RequestBody ArticleParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getCompanyId()) || StringUtils.isBlank(param.getCompanyName())
                || StringUtils.isBlank(param.getArticleTypeId()) || StringUtils.isBlank(param.getArticleTypeName()) || StringUtils.isBlank(param.getTitle())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        Article article = articleService.insert(param);
        if (article == null) {
            baseVo.setCode(Constant.RESPONSE_INSERT_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_INSERT_ERROR_MSG);
            return baseVo;
        }
        baseVo.setData(article.getId().toString());
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "update 文章", notes = "必填：id")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseVo update(@RequestBody ArticleParam param) {
        BaseVo baseVo = new BaseVo();
        if (param == null || StringUtils.isBlank(param.getId())) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (articleService.update(param)) {
            return baseVo;
        }
        baseVo.setCode(Constant.RESPONSE_UPDATE_ERROR_CODE);
        baseVo.setMsg(Constant.RESPONSE_UPDATE_ERROR_MSG);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "delete 文章 by id", notes = "必填：id")
    @RequestMapping(value = "deleteById", method = RequestMethod.DELETE)
    public BaseVo deleteById(@RequestParam(name = "id") String id) {
        BaseVo baseVo = new BaseVo();
        if (StringUtils.isBlank(id)) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        if (articleService.deleteById(Long.valueOf(id))) {
            return baseVo;
        }
        baseVo.setCode(Constant.RESPONSE_DELETE_ERROR_CODE);
        baseVo.setMsg(Constant.RESPONSE_DELETE_ERROR_MSG);
        return baseVo;
    }

    @ResponseBody
    @ApiOperation(value = "分页查询文章列表",
            notes = "1.需传入pageIndex >= 1(如：要显示第1页则传1), pageSize >= 1;" +
                    "2.返回分页查询列表;" +
                    "3.companyId和articleTypeId为选填参数;" +
                    "4.inputString为选填字段，按照标题title、副标题second_title、作者author模糊查询;")
    @RequestMapping(value = "select/selectAll", method = RequestMethod.POST)
    public BaseVo selectAllByExample(@RequestBody ArticleSelectParam param) {
        BaseVo baseVo = new BaseVo();
        if (param.getPageIndex() == null || param.getPageSize() == null || param.getPageIndex() < 1 || param.getPageSize() < 1) {
            baseVo.setCode(Constant.RESPONSE_PARAM_ERROR_CODE);
            baseVo.setMsg(Constant.RESPONSE_PARAM_ERROR_MSG);
            return baseVo;
        }
        List<Article> articleList = articleService.selectAllByExample(param);
        List<ArticleVo> articleVoList = new ArrayList<>();
        articleList.forEach(article -> {
            articleVoList.add(new ArticleVo(article));
        });
        int recordNumber = articleService.countRecordNumberByExample(param);
        ArticleAndRecordNumVo articleAndRecordNumVo = new ArticleAndRecordNumVo(recordNumber, articleVoList);
        baseVo.setData(articleAndRecordNumVo);
        return baseVo;
    }

}
